/**
 * 
 */
package View;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;

import Model.contact;

/**
 * @author enzo
 *
 */
public class ListView extends JPanel {
	private JLabel header;
	private JList contactList;
	private DefaultListModel listModel;
	private JScrollPane contactPan;
	private JPanel buttonPan;
	private JButton remove,add;
	public ListView(List<contact> list)
	{
		header=new JLabel("Contacts",SwingConstants.CENTER);
		Font headerFont = new Font("Serif", Font.BOLD, 20);
		header.setFont(headerFont);
		buttonPan=new JPanel();
		remove=new JButton("Remove");
		add=new JButton("Add");
		buttonPan.add(remove);
		buttonPan.add(add);
		contactList = new JList();
		contactList.setFixedCellHeight(75);
		contactList.setCellRenderer(new ContactCellRenderer());
		contactList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		 listModel= new DefaultListModel();
		 contactPan=new JScrollPane(contactList);
		 contactPan.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		 contactPan.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		 contactPan.setPreferredSize(new Dimension(220,300));
		 cloneContact(list);
		 this.setVisible(true);
		 this.setOpaque(true);
		 this.setLayout(new BorderLayout());
		 this.add("North",header);
		 this.add("Center",contactPan);
		 this.add("South",buttonPan);
	

	}
	
	public void cloneContact(List<contact> list)
	{
		listModel.removeAllElements();
		contactList.removeAll();
		for(contact c :list)
		{
			listModel.addElement(c);
		}

		contactList.setModel(listModel);
		contactList.setVisibleRowCount(list.size());
	}
	public void update()
	{
		contactPan.removeAll();
		contactPan.add(contactList);
	}

	/**
	 * @return the contactList
	 */
	public JList getContactList() {
		return contactList;
	}

	/**
	 * @return the remove
	 */
	public JButton getRemove() {
		return remove;
	}

	/**
	 * @return the add
	 */
	public JButton getAdd() {
		return add;
	}
}
